package com.viewpagertext.activitys;

import static com.viewpagertext.fragments.FriendFragment.nowUrl;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.annotation.SuppressLint;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

import com.viewpagertext.R;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class PortAllActivity extends AppCompatActivity implements SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener {

    private SurfaceView sv;
    private SurfaceHolder holder;
    private MediaPlayer mediaplayer;
    private ConstraintLayout cl;
    private Timer timer;
    private TimerTask task;
    private SeekBar sbar;
    private ImageView play;
    private Button back;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_port_all);
        sv = (SurfaceView) findViewById(R.id.sv);
        // 得到SurfaceView的容器,界面内容是显示在容器里面的
        cl = (ConstraintLayout) findViewById(R.id.cl);
        play = (ImageView) findViewById(R.id.play);
        sbar = (SeekBar) findViewById(R.id.sbar);
        back = findViewById(R.id.back);
        sbar.setOnSeekBarChangeListener(this);
        holder = sv.getHolder();
        // 过时的APT,如果4.0以上的系统不写没问题,否则必须要写
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(this);
        // 初始化计时器
        timer = new Timer();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        sv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (back.getVisibility() == View.GONE) {
                    back.setVisibility(View.VISIBLE);
                    play.setVisibility(View.VISIBLE);
                    sbar.setVisibility(View.VISIBLE);
                } else {
                    back.setVisibility(View.GONE);
                    play.setVisibility(View.GONE);
                    sbar.setVisibility(View.GONE);
                }

            }
        });

        task = new TimerTask() {
            @Override
            public void run() {
                if (mediaplayer != null && mediaplayer.isPlaying()) {
                    int total = mediaplayer.getDuration();
                    sbar.setMax(total);
                    int progress = mediaplayer.getCurrentPosition();
                    sbar.setProgress(progress);
                } else {
                    play.setImageResource(android.R.drawable.ic_media_play);
                }
            }
        };
        // 设置TimerTask延迟500ms,每隔500ms执行一次
        timer.schedule(task, 500, 500);
    }

    //播放暂停按钮的点击事件
    public void click(View view) {
        if (mediaplayer != null && mediaplayer.isPlaying()) {
            mediaplayer.pause();
            play.setImageResource(android.R.drawable.ic_media_play);
        } else {
            mediaplayer.start();
            play.setImageResource(android.R.drawable.ic_media_pause);
        }
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        try {
            mediaplayer = new MediaPlayer();
            mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            Uri uri = Uri.parse(nowUrl.getUrl());
            try {
                mediaplayer.setDataSource(PortAllActivity.this, uri);
            } catch (IOException e) {
                Toast.makeText(PortAllActivity.this, "播放失败",
                        Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
            mediaplayer.setDisplay(holder);
            mediaplayer.prepareAsync();
            mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaplayer.start();
                }
            });
        } catch (Exception e) {
            Toast.makeText(PortAllActivity.this, "播放失败",
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mediaplayer.isPlaying()) {
            mediaplayer.stop();
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int position = seekBar.getProgress();
        if (mediaplayer != null) {
            mediaplayer.seekTo(position);
        }
    }
}